print("------------ 存储属性 ------------")
class DataImporter {
    // 存储属性
    var fileName = "data.txt"
    init() {
       print("DataImporter inits")
    }
}

class DataManager {
    // 延迟存储属性(懒加载的属性)
    lazy var importer = DataImporter()
    var data = [String]()
}

let manager = DataManager()
manager.data.append("Some data")
manager.data.append("More data")
print("manager.data.append finished")
print(manager.importer.fileName)

print("------------ 计算属性 ------------")
struct Point {
    var x = 0, y = 0
}
struct Size {
    var width = 0, height = 0
}
struct Rect {
    var origin = Point()
    var size = Size()
    var center: Point {
        get {
            let centerX = origin.x + size.width / 2
            let centerY = origin.y + size.height / 2
            return Point(x: centerX, y: centerY)
        }
        set(newCenter) {
            origin.x = newCenter.x - size.width / 2
            origin.y = newCenter.y - size.height / 2
        }
    }
}
var r = Rect()
r.center = Point(x: 1, y: 2)
print(r)

print("------------ 属性观察者 ------------")
// 属性观察者也适用于全局变量和局部变量
class StepCounter {
    var totalSteps: Int = 0 {
        willSet(newTotoalSteps) {
            print("willSet - newTotoalSteps: \(newTotoalSteps)")
        }
        didSet(old) {
            // 如果写didSet {} 的话, 就只能使用oldValue来调用旧值
            print("didSet - totalSteps: \(totalSteps)")
            print("added: \(totalSteps - old)")
        }
    }
}
let stepCounter = StepCounter()
stepCounter.totalSteps = 200
stepCounter.totalSteps = 220
stepCounter.totalSteps = 250
